<template>
  <div>
    <el-empty v-if="dataSource.length === 0" :description="clickQuick.quickId !== '0' ? '暂未授权' : '暂未选择快捷菜单'" :style="{height: autoHeight }">
      <el-button v-if="clickQuick.quickId !== '0'" type="primary" @click="visible = true" icon="iconfont icon-auth-user"></el-button>
    </el-empty>
    <el-table v-else v-loading="loading" :data="dataSource" highlight-current-row @row-click="handleRowClick" @row-contextmenu="rightClick" :height="autoHeight">
      <el-table-column label="操作" align="center" width="80px">
        <template slot="header" slot-scope="scope" >
          <el-button v-if="scope && clickQuick.quickId !== '0'" type="text" @click="visible = true" icon="iconfont icon-auth-user"></el-button>
          <span v-else>操作</span>
        </template>
        <div slot-scope="scope">
          <el-link type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-link>
        </div>
      </el-table-column>
      <el-table-column prop="authType" label="类型" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-select v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)">
            <el-option v-for="item in authTypes" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </template>
        <span slot-scope="scope">{{ authTypes.find(item => item.value === scope.row.authType).label}}</span>
      </el-table-column>
      <el-table-column prop="authLabel" label="对象" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, prev, next"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>

    <el-dialog :visible.sync="visible" title="选择授权对象" width="70%">
      <el-tabs v-model="activeName">
        <el-tab-pane label="选择用户" name="user" lazy>
          <user-branch @rowSelect="selectObj" />
        </el-tab-pane>
        <el-tab-pane label="选择岗位" name="job" lazy>
          <job-table select @rowSelect="selectObj" />
        </el-tab-pane>
        <el-tab-pane label="选择角色" name="role" lazy>
          <role-table select @rowSelect="selectObj" />
        </el-tab-pane>
        <el-tab-pane label="选择组织" name="group" lazy>
          <branch-table @rowSelect="selectObj" />
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";
import UserBranch from "@/views/system/user/UserBranch";
import JobTable from "@/views/system/job/JobTable";
import RoleTable from "@/views/system/sign/SignRecord";
import BranchTable from "@/views/system/group/BranchTable";

export default {
  name: "AppAuthTable",
  components: {BranchTable, RoleTable, JobTable, UserBranch},
  mixins: [IdeaTable],
  inject: {
    appContext: {
      from: 'appContext',
      default: function () {
        return {}
      }
    }
  },
  props: {
    select: {
      type: Boolean,
      default: false
    },
    clickQuick: {
      type: Object,
      default: function () {
        return { quickId: 0 }
      }
    }
  },
  data() {
    return {
      authTypes: [{value:'user',label:'用户'},{value:'job',label:'岗位'},{value:'role',label:'角色'},{value:'group',label:'组织'}],
      activeName: 'user'
    }
  },
  created() {
    this.queryUrl = '/appAuth/queryPage'
    this.saveUrl = '/appAuth/save'
    this.removeUrl = '/appAuth/delete/'
    this.rowKey = 'appAuthId'
    this.pageInfo.pageSize = 20
    this.addFilter({ field: 'appId', op: '=', value: this.appInfo.appId})
    this.refreshTable()
  },
  computed: {
    autoHeight() {
      return this.select ? `580px` : `calc(100vh - 398px)`
    }
  },
  methods: {
    refreshTable() {
      this.addFilter({ field: 'quickId', op: '=', value: this.clickQuick.quickId })
      this.loadSource()
    },
    selectObj(row){
      const authObj = { authType: this.activeName, quickId: this.clickQuick.quickId  }
      if (this.activeName === 'user') {
        authObj.authValue = row.userId
        authObj.authLabel = row.nickName
      } else if (this.activeName === 'job') {
        authObj.authValue = row.jobId
        authObj.authLabel = row.jobName
      } else if (this.activeName === 'role') {
        authObj.authValue = row.roleId
        authObj.authLabel = row.roleName
      } else if (this.activeName === 'group') {
        authObj.authValue = row.branchId
        authObj.authLabel = row.groupAllName
      } else {
        this.$resultMsg(this, { msg: '未知类型'})
        return
      }
      this.saveData(authObj, true)
    }
  }
}
</script>

<style scoped>

</style>
